<?php

/**
 * modulesformation actions.
 *
 * @package    espace14
 * @subpackage modulesformation
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class modulesformationActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
	$results_per_page = 10;
    $first_page=1;  
    $this->pagination = new sfDoctrinePager('modules', $results_per_page);  
    $this->pagination->setQuery(Doctrine_Core::getTable('modules')->createQuery('a'));  
    $this->pagination->setPage($request->getParameter('page', $first_page));  
    $this->pagination->init();  
	$this->moduless = $this->pagination->getResults();
	
	$this->getResponse()->setTitle('Espace 14 | Programmes de formation');
   // $this->moduless = Doctrine_Core::getTable('modules')
   //  ->createQuery('a')
   //   ->execute();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->modules = Doctrine_Core::getTable('modules')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->modules);
  }

  public function executeNew(sfWebRequest $request)
  {
	 $this->form = new modulesForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));
	$this->form = new modulesForm();
	$temp = $request->getParameter('editor1');
	//$this->form->setDefault("description", $temp);
    $this->processForm($request, $this->form);

    $this->setTemplate('new');
	
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($modules = Doctrine_Core::getTable('modules')->find(array($request->getParameter('id'))), sprintf('Object modules does not exist (%s).', $request->getParameter('id')));
    $this->form = new modulesForm($modules);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($modules = Doctrine_Core::getTable('modules')->find(array($request->getParameter('id'))), sprintf('Object modules does not exist (%s).', $request->getParameter('id')));
    $this->form = new modulesForm($modules);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($modules = Doctrine_Core::getTable('modules')->find(array($request->getParameter('id'))), sprintf('Object modules does not exist (%s).', $request->getParameter('id')));
    $modules->delete();

    $this->redirect('modulesformation/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
	
    if ($form->isValid())
    {	
		
			$page = $form->save();
			$page->setDescription($request->getParameter('editor1'));
			$form = $page->save();
		
      //$this->redirect('modulesformation/edit?id='.$modules->getId());
	  $this->redirect('modulesformation/index');
    }
	
  }
}
